/**************************************************************
 * Ben Stephenson 07025656
 * CM0034 Industrial Case Project
 * Control Panel
 * state.c
 * 18/03/2011
 **************************************************************/

/**************************************
 ************ PRE-PROCESSOR************
 *^z***********************************/
#include <state.h>

/*z^***********************************/


static int state;
static int nodeState[5];

/**************************************
 ************* FUNCTIONS **************
 *^z***********************************/

/*****************************************
 * Function : initState
 * Desc     : Initialise status variables
 * Return   : Void
 * Params   : void
 *^z***************************************/
void initState(void)
{
   state = INIT;

   nodeState[ID_ALLNODES]  = NODE_STOPPED;
   nodeState[ID_ARM1]      = NODE_STOPPED;
   nodeState[ID_ARM2]      = NODE_STOPPED;
   nodeState[ID_CONVEYOR]  = NODE_STOPPED;

   // If we have started up, we are ready to run
   nodeState[MY_ID]        = NODE_READY;
}
/*z^***************************************/


/*****************************************
 * Function : getState
 * Desc     : Return system state
 * Return   : int
 * Params   : void
 *^z***************************************/
int getState()
{
   return state;
}
/*z^***************************************/


/*****************************************
 * Function : setState
 * Desc     : Sets state of a variable
 * Return   : void
 * Params   : int
 *^z***************************************/
void setState(int status)
{
   state = status;
}
/*z^***************************************/


/*****************************************
 * Function : getNodeState
 * Desc     : Return state of a node
 * Return   : int
 * Params   : int
 *^z***************************************/
int getNodeState(int var)
{
   return nodeState[var];
}
/*z^***************************************/


/*****************************************
 * Function : determineState
 * Desc     : Check node states to determine
 *            if we need to change sys state
 * Return   : void
 * Params   : int
 *^z***************************************/
void determineState(int state)
{
   if (getNodeState(MY_ID)       == state  &&
       getNodeState(ID_ARM1)     == state  &&
       getNodeState(ID_ARM2)     == state  &&
       getNodeState(ID_CONVEYOR) == state)
   {
      // We're all paused, so set the system state
      setState(state);
   }
}
/*z^***************************************/


/*****************************************
 * Function : setNodeState
 * Desc     : Sets state of a node
 * Return   : void
 * Params   : int, int
 *^z***************************************/
void setNodeState
(
   int   var,
   int  status
)
{
   nodeState[var] = status;
}
/*z^***************************************/
/*z^***********************************/

/******************************************* EOF *************************************************/
